﻿using System;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.BLL.BllExt;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;
using Matrix.MaKaYunShang.Model.Enum;

namespace Matrix.MaKaYunShang.McMagWeb.Controllers
{
    /// <summary>
    /// 商家提现
    /// </summary>
    [Authorization]
    public class WithdrawalsController : BaseController
    {
        public OrderExtBll OrderExtBllObj = new OrderExtBll();

        [PermissionFilter]
        public ActionResult List()
        {
            double totalWithdrawCashByShopId = 0;
            double nCashWithdrawalAmount = 0;
            //try
            //{
            //    var lsWithDrawHistoryOfShop =
            //    McCashWithdrawApplicationBLL.Instance.GetTotalWithdrawCashByShopId(CurrentUser.ShopId);
            //    if (lsWithDrawHistoryOfShop != null && lsWithDrawHistoryOfShop.Count > 0)
            //    {
            //        totalWithdrawCashByShopId = lsWithDrawHistoryOfShop.Sum(p => p.CashValue);
            //    }
            //    nCashWithdrawalAmount = OrderExtBllObj.GetCashWithdrawalAmount(totalWithdrawCashByShopId,
            //    CurrentUser.ShopId);
            //}
            //catch (Exception ex)
            //{
            //    Logger.Error("WithdrawalsController | List | Catch Error: " + ex.Message);
            //}
            var totalIncomeOfShopId = ShopsBLL.Instance.GetTotalIncomeOfShopId(CurrentUser.ShopId);
            var totalShopCommission = ShopsBLL.Instance.GetTotalShopCommission(CurrentUser.ShopId);
            var lsWithDrawHistoryOfShop = McCashWithdrawApplicationBLL.Instance.GetTotalWithdrawCashByShopId(CurrentUser.ShopId);
            if (lsWithDrawHistoryOfShop != null && lsWithDrawHistoryOfShop.Count > 0)
            {
                totalWithdrawCashByShopId = lsWithDrawHistoryOfShop.Sum(p => p.CashValue);
            }
             nCashWithdrawalAmount = (totalIncomeOfShopId + totalShopCommission) - totalWithdrawCashByShopId;//可提现金额
            ViewBag.DealStatus = -1;
            ViewBag.totalIncomeOfShopAmount = totalIncomeOfShopId + totalShopCommission;//总收入金额
            ViewBag.totalWithdrawCashByShopId = totalWithdrawCashByShopId;
            ViewBag.CashWithdrawalAmount = nCashWithdrawalAmount;//当前店铺可提下金额
            return View();
        }

        [HttpPost]
        public JsonResult Listing(int draw, int start, int length, int dealStatus = -1)
        {
            try
            {
                int order;
                int.TryParse(Request["order[0][column]"], out order);
                string search = Request["search[value]"];
                string direction = Request["order[0][dir]"];
                var orderByColumns = new[] { "ApplyTime", "BankAccountName", "BankOfDeposit", "BankName", "BankAccount", "DealStatus" };
                TableDataViewDto<ViewMcCashWithdrawApplicationDto> theList = McCashWithdrawApplicationBLL.Instance.GetPagingList(draw, (start / length) + 1, length, SqlSafe(search),
                orderByColumns[order] + " " + direction, dealStatus, CurrentUser.ShopId);
                JsonResult jsonResult = Json(theList);
                jsonResult.MaxJsonLength = int.MaxValue;

                return jsonResult;
            }
            catch (Exception ex)
            {
                Logger.Error("WithdrawalsController Listing Catch Error: " + ex.Message);
                return Json(false);
            }
        }
        public ActionResult WithdrawOperation()
        {
            return View();
        }
        [HttpPost]
        public ActionResult WithdrawOperation(McCashWithdrawApplication model)
        {
            var result = new Result { Message = "提现失败", Status = false, StatusCode = 0 };
            try
            {
                double totalWithdrawCashByShopId = 0;
                var totalIncomeOfShopId = ShopsBLL.Instance.GetTotalIncomeOfShopId(CurrentUser.ShopId);
                var totalShopCommission = ShopsBLL.Instance.GetTotalShopCommission(CurrentUser.ShopId);
                var lsWithDrawHistoryOfShop = McCashWithdrawApplicationBLL.Instance.GetTotalWithdrawCashByShopId(CurrentUser.ShopId);
                if (lsWithDrawHistoryOfShop != null && lsWithDrawHistoryOfShop.Count > 0)
                {
                    totalWithdrawCashByShopId = lsWithDrawHistoryOfShop.Sum(p => p.CashValue);
                }
                var nCashWithdrawalAmount = (totalIncomeOfShopId + totalShopCommission) - totalWithdrawCashByShopId;//可提现金额
                if (nCashWithdrawalAmount < 1 || model.CashValue > nCashWithdrawalAmount)
                {
                    result.Status = false;
                    result.Message = "提现额度不足!";
                    return Json(result);
                }
                if (model.CashValue < 100)
                {
                    result.Status = false;
                    result.Message = "最低提现金额为100元!";
                    return Json(result);
                }

                var shops = ShopsBLL.Instance.GetModel(CurrentUser.ShopId);
                if (shops == null)
                {
                    Logger.Error("WithdrawalsController WithdrawOperation  Error: 店铺信息错误!");
                    return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
                }
                model.Remark = model.Remark;
                model.ApplyTime = DateTime.Now;
                model.DealStatus = (int)CommonEnum.McWithdrawDealStatus.Pending;
                model.ApplyAccountId = CurrentUser.AccountId;
                model.ShopId = CurrentUser.ShopId;
                if (McCashWithdrawApplicationBLL.Instance.Add(model) > 0)
                {
                    result.Status = true;
                    result.Message = "提现成功,请等待后台审核!";
                }
            }
            catch (Exception ex)
            {
                Logger.Error("WithdrawalsController | WithdrawOperation | Catch Error: " + ex.Message);
            }

            return Json(result);
        }


    }
}